﻿using Module20240909.Event;
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Module20240909.ViewModels
{
    public class ViewDialogViewModel : IDialogAware
    {
        private readonly IEventAggregator aggregator;

        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }
        public ViewDialogViewModel(IEventAggregator aggregator)
        {
            CancelCommand = new DelegateCommand(Cancel);
            SaveCommand = new DelegateCommand(Save);
            this.aggregator = aggregator;
        }

        private void Save()
        {
            Test test = new Test();
            test.Name = "CCC";
            test.Age = "13";
            //使用MessageEvent发布一个订阅
            aggregator.GetEvent<TestEvent>().Publish(test);
            RequestClose?.Invoke(new DialogResult(ButtonResult.Yes));
            //RequestClose?.Invoke(new DialogResult(ButtonResult.Yes));
            OnDialogClosed();
        }

        private void Cancel()
        {
            //使用MessageEvent发布一个订阅
            aggregator.GetEvent<MessageEvent>().Publish("发布订阅了呢！");
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }


        public string Title { get; set; }
    

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            DialogParameters keys = new DialogParameters();
            keys.Add("value", "传入主界面");
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK,keys));
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Title = parameters.GetValue<string>("Title");
        }
    }
}
